<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<div class="panel panel-default operation_class" hidden>
  <div class="panel-heading title"></div>
<div class="layui-card-body">
<form class="layui-form " action="" lay-filter="info" style="width: 700px;margin-top: 10px">
    <input name="id" hidden/>
    <input name="pId" hidden/>
    <div class="layui-form-item">
      <label class="layui-form-label">分类名称</label>
      <div class="layui-input-block">
        <input type="name" name="name" placeholder="请输入分类名称" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-form-label ">所属部门</div>
      <div class="layui-input-block">
        <input type="pidName" name="pIdName" placeholder="请选择所属部门" autocomplete="off" class="layui-input"
               readonly="readonly" style="background:#eeeeee!important">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">负责人</label>
      <div class="layui-input-block">
        <input type="director" name="director" placeholder="请输入负责人" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">预算金额</label>
      <div class="layui-input-block">
        <input type="budget" name="budget" placeholder="请输入预算（单位：万元）" autocomplete="off" class="layui-input">
      </div>
    </div>
  <div class="layui-form-item">
  <div class="layui-input-block">
    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
    <button  class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
  </div>
</div>
</form>
</div>
</div>

<div class="class-table">
  <table class="layui-table" id="classTable" lay-filter="classTable"></table>
</div>
<div id="classTree" style="display: none"></div>
<script type="text/html" id="toolbar">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="addClass"  shiro:hasPermission="sysClassification:add">添加</button>
  </div>
</script>
<script type="text/html" id="tool">
  <a class="layui-btn layui-btn-xs" lay-event="edit" shiro:hasPermission="sysClassification:update">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="sysClassification:delete">删除</a>
</script>

</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
     var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var form = layui.form;
    var element = layui.element;
    var treetable;
    var selectNode = {};
    layui.config({
        base: '/layui-ext/'
    }).extend({
        treetable: 'treetable-lay/treetable'
    }).use(['treetable', 'table', 'tree'], function () {
        var reloadTreeTable = function () {
            CoreUtil.sendGet("/sysClassifications", null, function (res) {
                renderTable(res.data);
            });
        };
        reloadTreeTable();
        treetable = layui.treetable;
        var table = layui.table;
        var tree = layui.tree;
        var renderTable = function (data) {
            treetable.render({
                // 渲染表格
                data: data,
                treeColIndex: 1, // 树形图标显示在第几列
                treeSpid: 0, // 最上级的父级id
                treeIdName: 'id', // id字段的名称
                treePidName: 'pId', // pid字段的名称
                treeDefaultClose: false, // 是否默认折叠
                treeLinkage: true, // 父级展开时是否自动展开所有子级
                elem: '#classTable', // 表格id
                page: false, // 树形表格一般是没有分页的
                toolbar: '#toolbar',
                cols: [
                    [
                        {type: 'numbers'},
                        {field: 'name', title: '分类名称',},
                        {
                            field: 'pIdName', title: '上级分类名称', templet: function (item) {
                                if (item.pIdName === null || item.pIdName === undefined) {
                                    return "最顶级分类";
                                } else {
                                    return item.pIdName;
                                }
                            }
                        },
                        {field: 'director', title: '负责人'},
                        {field: 'budget', title: '预算金额（万元）'},
                        {title: '操作', width: 250, toolbar: '#tool'}
                    ]
                ]
            });

        };
        //操作项事件
        table.on('tool(classTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    deletedClass(data.id, data.name);
                    break;
                case 'edit':
                    selectNode = null;
                    $(".class-table").hide();
                    $(".operation_class").show();
                    $(".title").html("编辑部门");
                    $(".operation_class input[name=id]").val(data.id);
                    $(".operation_class input[name=pId]").val(data.pId);
                    $(".operation_class input[name=name]").val(data.name);
                    $(".operation_class input[name=pIdName]").val(data.pIdName);
                    $(".operation_class input[name=director]").val(data.director);
                    $(".operation_class input[name=budget]").val(data.budget);
                    form.render(); //更新全部
                    initTree(data.id);
                    break;
            }
        });
        table.on('toolbar(classTable)', function (obj) {
            switch (obj.event) {
                case 'addClass':
                    selectNode = null;
                    $(".class-table").hide();
                    $(".operation_class").show();
                    $(".title").html("新增分类");
                    $(".operation_class input[name=id]").val("");
                    $(".operation_class input[name=pId]").val("");
                    $(".operation_class input[name=name]").val("");
                    $(".operation_class input[name=pIdName]").val("");
                    $(".operation_class input[name=director]").val("");
                    $(".operation_class input[name=budget]").val("");
                    form.render(); //更新全部
                    initTree("");
                    break;
            }
        });
        var deletedClass = function (classId, menuName) {
            layer.open({
                content: '确定要删除' + menuName + "?",
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendDelete("/sysClassification/" + classId, null, function (res) {
                        layer.msg(res.msg);
                        reloadTreeTable();
                    });
                }
            });
        };
        $("#btn_cancel").click(function () {
            $(".class-table").show();
            $(".operation_class").hide();
            return false;
        });
        var loadClassTree = function (data) {
            tree.render({
                elem: '#classTree'
                , data: data
                , onlyIconControl: true  //是否仅允许节点左侧图标控制展开收缩
                , click: function (obj) {
                    selectNode = obj;
                    layer.msg(JSON.stringify(selectNode.data.title));
                }
            });
        };
        $(".operation_class input[name=pIdName]").click(function () {
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: "选择分类",
                area: ['400px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#classTree"),
                btn: ['确定', '取消'],
                yes: function (index) {
                    if (selectNode.data != null) {
                        //选中回显
                        $(".operation_class input[name=pId]").val(selectNode.data.id);
                        $(".operation_class input[name=pIdName]").val(selectNode.data.title);
                    }

                    layer.close(index);
                }
            });
        });
        form.on('submit(submit)', function (data) {
            if (data.field.id === undefined || data.field.id === null || data.field.id === "") {
                CoreUtil.sendPost("/sysClassification", data.field, function (res) {
                    reloadTreeTable();
                    $(".class-table").show();
                    $(".operation_class").hide();
                });
            } else {
                CoreUtil.sendPut("/sysClassification", data.field, function (res) {
                    reloadTreeTable();
                    $(".class-table").show();
                    $(".operation_class").hide();
                });
            }

            return false;
        });
        var initTree = function (id) {
            var param = {classId: id}
            CoreUtil.sendGet("/sysClassification/tree", param, function (res) {
                loadClassTree(res.data);
            });
        };

    });
</script>